<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 练习2：有已知以下某公司员工薪金信息，把每一位员工的salary薪金信息 加 2000，并返回新数组，注意：不影响原数组
        var persons = [{
            username: '张飞',
            sex: '男',
            salary: 50000
        },
        {
            username: '关羽',
            sex: '男',
            salary: 60000
        }]

        // 1
        var obj1 = JSON.parse(JSON.stringify(persons));
        obj1 = obj1.map(function (value) {
            for (var key in value) {
                if (key == 'salary') {
                    value[key] += 2000;
                    return value;
                }
            }
        })
        console.log(obj1);
        console.log(persons);

        // 2
        var obj2 = JSON.parse(JSON.stringify(persons));
        obj2 = obj2.map(function (value) {
            value.salary += 2000;
            return value;
        })
        console.log(obj2);
        console.log(persons);

        // 3
        var arr = [];
        for (var i = 0; i < persons.length; i++) {
            if (typeof (persons[i]) == 'object') {
                arr.push({});
                for (var key in persons[i]) {
                    arr[i][key] = persons[i][key];
                    arr[i].salary += 2000;
                }
            }
        }
        console.log(arr);
        console.log(persons);
    </script>
</body>

</html>